'use strict';

module.exports = app => {
  const { STRING, INTEGER, BOOLEAN, DATE } = app.Sequelize;

  const Collection = app.model.define('collection', {
      id:{
        type: INTEGER,
        primaryKey: true,
        allowNull: false,
        autoIncrement: true
      },
      // 收藏夹名
      title: {
        type: STRING,
        allowNull: false
      },
      // 收藏夹描述（非必填）
      description: {
        type: STRING,
        allowNull: false,
        defaultValue: '请填写你的收藏夹描述吧~'
      },
      // 是否公开（默认公开），私有别人不可看
      public: {
        type: BOOLEAN,
        allowNull: false,
        defaultValue: true
      },
      created_at: DATE,
      updated_at: DATE
  });

  Collection.associate = function () {
    app.model.Collection.belongsToMany(app.model.Article, {
      through: app.model.ArticleCollection,
      foreignKey: 'collection_id',
      otherKey: 'article_id'
    })
    app.model.Collection.belongsTo(app.model.User, {
      foreignKey: {
        name: 'owner_id',
        allowNull: true
      },
      targetKey: 'id'
    })
  };

  return Collection;
}